package com.sdy.resdir.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.resdir.biz.model.RdImplementationOrder;
import com.sdy.resdir.biz.model.RdResourceDir;
import com.sdy.resdir.biz.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private RdResourceDirService rdResourceDirService;
    @Autowired
    private RdImplementationOrderService implementationOrderService;
    @Autowired
    private RdResForOnlineService rdResForOnlineService;
    @Autowired
    private RdResourceApplicationDetailService rdResourceApplicationDetailService;
    @Value("${res.app.code}")
    private String appCode;

    @Override
    public Response updateResInfo(RdResourceDir res, Date date, Integer userId) throws Exception {
        Assert.isNull(res.getId(), "资源id不可为空");
        boolean b = rdResourceDirService.updateById(res);
        implementationOrderService.update(new LambdaUpdateWrapper<RdImplementationOrder>()
                .eq(RdImplementationOrder::getResId, res.getId())
                .set(RdImplementationOrder::getResName, res.getResName()));
        // 准备需要的数据
        Map<String, Object> map = new HashMap<>(16);
        map.put("deptId", res.getResPowerDeptId());
        map.put("resId", res.getId());
        map.put("resPower", res.getResPower());
        // 判断资源是未归集还是已归集
        Integer collectionSituation = res.getCollectionSituation();
        // 如果未发布 且deptId空 则无需配置模板
        RdResourceDir byId = rdResourceDirService.getById(res.getId());
        Integer isRelease = byId.getIsRelease();
        if (isRelease == 0 && null == res.getResPowerDeptId()) {
            return Response.success();
        }

        // (collectionSituation 1-已归集  2-未归集)
        Boolean aBoolean;
        if (collectionSituation == 1) {
           aBoolean = rdResourceApplicationDetailService.addApprovalConfigDetail(map, date, userId, 1);
            Assert.notTrue(aBoolean, "修改发布数据出错!");
        } else {
            aBoolean = rdResForOnlineService.addConfigForOnline(map, date, userId, 1);
        }
        Assert.notTrue(aBoolean, "修改发布数据出错!");
        return Response.success();
    }
}
